package com.ruoyi.life.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.life.mapper.FeBillMapper;
import com.ruoyi.life.mapper.FeFineMapper;
import com.ruoyi.life.mapper.FeLeaseRecordMapper;
import com.ruoyi.life.service.IFeBillService;
import com.ruoyi.omp.api.RemoteRoomService;
import com.ruoyi.omp.api.domain.FeBill;
import com.ruoyi.omp.api.domain.FeFine;
import com.ruoyi.omp.api.domain.vo.RoomVO;
import com.ruoyi.omp.api.enums.FeeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 应收账单Service业务层处理
 *
 * @author cjie
 * @date 2021-09-23
 */
@Service
public class FeBillServiceImpl implements IFeBillService {
    @Autowired
    private FeBillMapper feBillMapper;
    @Autowired
    private FeFineMapper feFineMapper;

    @Autowired
    private FeLeaseRecordMapper feLeaseRecordMapper;
    /**
     * 查询应收账单
     * @param id 应收账单ID
     * @return 应收账单
     */
    @Override
    public FeBill selectFeBillById(String id) {
        FeBill feBill = feBillMapper.selectFeBillById(id);
        feBill.setTotalFine(feFineMapper.sumByBillId(id));
        if (feBill.getTotalFine()==null||feBill.getTotalFine().equals("")){
            feBill.setTotalFine(new BigDecimal("0"));
        }
        //通过租赁id查找房源
        List<RoomVO> roomVOList = feLeaseRecordMapper.selectRoomByRecordId(feBill.getRecordId());
        feBill.setRooms(roomVOList);
        FeFine queryFine = new FeFine();
        queryFine.setBillId(feBill.getId());
        feBill.setFineList(feFineMapper.selectFeFineList(queryFine));
        return feBill;
    }


    /**
     * 查询应收账单列表
     *
     * @param feBill 应收账单
     * @return 应收账单
     */
    @Override
    public List<FeBill> selectFeBillList(FeBill feBill) {
        List<FeBill> feBills = feBillMapper.selectFeBillList(feBill);
        feBills.forEach(bill->bill.setTotalFine(feFineMapper.sumByBillId(bill.getId())));
        return feBills;
    }

    /**
     * 新增应收账单
     *
     * @param feBill 应收账单
     * @return 结果
     */
    @Override
    public int insertFeBill(FeBill feBill) {
        feBill.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        feBill.setFeeTypeName(FeeTypeEnum.getFeeTypeEnumByCode(feBill.getFeeType()).getType());
        feBill.setCreateTime(DateUtils.getNowDate());
        feBill.setCreateBy(SecurityUtils.getUsername());
        return feBillMapper.insertFeBill(feBill);
    }

    /**
     * 修改应收账单
     *
     * @param feBill 应收账单
     * @return 结果
     */
    @Override
    public int updateFeBill(FeBill feBill) {
        feBill.setUpdateTime(DateUtils.getNowDate());
        feBill.setUpdateBy(SecurityUtils.getUsername());

        //固废已在生产账单时计算，无法修改，电梯费用前端累加，所有注释掉费用重算

        //BigDecimal receivableAmount = feBill.getReceivableAmount();
        //if(feBill.getSurety() != null){
        //    receivableAmount = receivableAmount.add(feBill.getSurety());
        //}
        //if(feBill.getGarbageFee() != null){
        //    receivableAmount = receivableAmount.add(feBill.getGarbageFee());
        //}
        //if(feBill.getSewageFee() != null){
        //    receivableAmount = receivableAmount.add(feBill.getSewageFee());
        //}
        //if(feBill.getElevatorRunFee() != null){
        //    receivableAmount = receivableAmount.add(feBill.getElevatorRunFee());
        //}
        //if(feBill.getElevatorRepairFee() != null){
        //    receivableAmount = receivableAmount.add(feBill.getElevatorRepairFee());
        //}

        //feBill.setReceivableAmount(receivableAmount);
        return feBillMapper.updateFeBill(feBill);
    }

    /**
     * 批量删除应收账单
     *
     * @param ids 需要删除的应收账单ID
     * @return 结果
     */
    @Override
    public int deleteFeBillByIds(String[] ids) {
        return feBillMapper.deleteFeBillByIds(ids);
    }

    /**
     * 删除应收账单信息
     *
     * @param id 应收账单ID
     * @return 结果
     */
    @Override
    public int deleteFeBillById(String id) {
        return feBillMapper.deleteFeBillById(id);
    }

    /**
     * 查询对应租赁记录及费用类型的最后一条记录
     *
     * @param feBill 收费账单
     * @return
     */
    @Override
    public FeBill selectLastBillByRecordIdAndFeeType(FeBill feBill) {
        return feBillMapper.selectLastBillByRecordIdAndFeeType(feBill);
    }

    /**
     * 查询所有已过截止期限但未付清的账单
     *
     * @param feBill 账单
     * @return
     */
    @Override
    public List<FeBill> listBill(FeBill feBill) {
        return feBillMapper.selectFeBillList(feBill);
    }
}
